#include "SpriteCoconut.h"

CSpriteCoconut::CSpriteCoconut(void)
{
	setSRbodyType(CSRBODY_TYPE_COCONUT);
	changeState(COCONUT_STATE_NORMAL);
}

CSpriteCoconut::~CSpriteCoconut(void)
{
}

CSpriteCoconut* CSpriteCoconut::spriteWithFile( const char *pszFileName )
{
	CSpriteCoconut *pobSprite = new CSpriteCoconut();
	if (pobSprite && pobSprite->initWithFile(pszFileName))
	{
		pobSprite->autorelease();
		return pobSprite;
	}
	CC_SAFE_DELETE(pobSprite);
	return NULL;
}

CSpriteCoconut* CSpriteCoconut::spriteWithRBodyFile( float density,const CCPoint& pos,const char *pszFileName )
{
	CSpriteCoconut* spr = spriteWithFile(pszFileName);

	if (spr)
	{

		spr->setPosition(pos);

		CCSize boxSize = CCSizeMake(spr->getContentSizeInPixels().width,spr->getContentSizeInPixels().height);

		b2BodyDef paddleBodyDef;

		paddleBodyDef.userData = spr;

		paddleBodyDef.type = b2_dynamicBody;
		paddleBodyDef.position.Set(spr->getPositionInPixels().x/PTM_RATIO,spr->getPositionInPixels().y/PTM_RATIO);
		b2Body *m_paddleBody = Cbox2dManage::s_world->CreateBody(&paddleBodyDef);

		b2CircleShape paddleShape;
		paddleShape.m_radius = boxSize.width/PTM_RATIO/2.0f;

		b2FixtureDef paddleShapeDef;
		paddleShapeDef.shape = &paddleShape;
		paddleShapeDef.density = density;

		paddleShapeDef.filter.categoryBits = CATEGORYBITS_COCONUT;
		paddleShapeDef.filter.maskBits = MASKBITS_COCONUT;

		spr->setFixture(m_paddleBody->CreateFixture(&paddleShapeDef));
		Cbox2dManage::s_Buoyancy->AddBody(m_paddleBody);

	}

	return spr;
}

void CSpriteCoconut::updatelogic()
{

}

void CSpriteCoconut::updateHitLogic()
{
	switch(getState())
	{
	case COCONUT_STATE_NORMAL:
		{
			CCLog("not hit");

		}
		break;
	case COCONUT_STATE_DESTROY:
		{
			DestroyBuoyancyRbody();
			
			this->removeFromParentAndCleanup(true);
			
			CCLog("hit But");
		}
		break;
	}
}
void CSpriteCoconut::sethitType(int type)
{
	__super::sethitType(type);
	if (type == CSRBODY_TYPE_BOTTOM)
	{
		changeState(COCONUT_STATE_DESTROY);
	}
	else
	{
		
	}
}

void CSpriteCoconut::changeState(int state)
{
	m_state = state;
}
int CSpriteCoconut::getState()
{
	return m_state;
}